package com.multiplayer.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.multiplayer.dao.IUserDao;
import com.multiplayer.dao.impl.UserDaoImpl;
import com.multiplayer.entity.Reservation;
import com.multiplayer.entity.Seat;
import com.multiplayer.entity.Student;
import com.multiplayer.entity.StudyRoom;
import com.multiplayer.entity.User;
import com.multiplayer.tools.DBTools;

/**  
 * Servlet implementation class PersonalServlet
 */
@WebServlet("/PersonalServlet")
public class PersonalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PersonalServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//post请求中文乱码解决
	 	request.setCharacterEncoding("utf-8");
	 	response.setContentType("text/html; charset=UTF-8");
	 	response.setCharacterEncoding("utf-8");
	 	//获取操作
	 	String op=request.getParameter("op");
	    if("updatePwd".equals(op)){
	    	updatePwd(request, response);
		}else if("judjeOldPwd".equals(op)){
			judjeOldPwd(request,response);
		}else if("judjeNPwd".equals(op)){
			judjeNPwd(request,response);
		}else if("quaryUserRes".equals(op)){
			quaryUserRes(request,response);
		}else if("updateUserInfo".equals(op)){
			updateUserInfo(request,response);
		}
	}

	private void updateUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取修改信息
		String sno=request.getParameter("sno");
		String sname=request.getParameter("sname");
		int sage=Integer.parseInt(request.getParameter("sage"));
		String sclass=request.getParameter("sclass");
		String sacademy=request.getParameter("sacademy");
		//执行修改
		int exUpdate = DBTools.exUpdate("update student set sname=?,sage=?,sclass=?,sacademy=? where sno=? ", sname,sage,sclass,sacademy,sno);
		if(exUpdate>0){
			request.setAttribute("updateResult", "修改个人信息成功");
			request.getRequestDispatcher("personal/crowdfunding.center/my_info.jsp").forward(request, response);
		}else{
			request.setAttribute("updateResult", "修改个人信息失败");
			request.getRequestDispatcher("personal/crowdfunding.center/my_info.jsp").forward(request, response);
			
		}
	}

	private void quaryUserRes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取用户id
		String userid=request.getParameter("userid");
		System.out.println(userid);
		//根据userid 查询预定信息
		List<Reservation> userReservation = (List<Reservation>)DBTools.exQuery("select * from reservation where userid=?", Reservation.class, userid);
		System.out.println(userReservation);
		//查询座位信息
		for(Reservation reservation:userReservation){
			String eseatno=reservation.getEseatno();
			//根据座位id查询自习室位置
			List<Seat> seatlist = (List<Seat>)DBTools.exQuery("select * from seat where eseatno=?", Seat.class, eseatno);
			for(Seat seat:seatlist){
			String rno=seat.getRno();
			//根据自习室id查询自习室位置
			List<StudyRoom> studyRoomList=(List<StudyRoom>)DBTools.exQuery("select * from studyroom where rno=?", StudyRoom.class, rno);
		    for(StudyRoom studyRoom:studyRoomList){
			String rposition=studyRoom.getRposition();
		    request.setAttribute("rposition", rposition);
		}
		}
		}
		request.setAttribute("userReservation", userReservation);
		request.getRequestDispatcher("personal/crowdfunding.center/orderRecord.jsp").forward(request, response);
	}

	private void judjeNPwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		Gson gson=new Gson();
		PrintWriter out=response.getWriter();
		//获取前端返回的新密码和重复密码
		String newPwd=request.getParameter("newPwd");
		String rNewPwd=request.getParameter("rNewPwd");
		System.out.println(newPwd);
		System.out.println(rNewPwd);
		System.out.println(newPwd.equals(rNewPwd));
		if(newPwd.equals(rNewPwd)){
			System.out.println("新密码两次输入一致！");
			String str=gson.toJson("新密码两次输入一致！");
			out.print(str);
		}else{
			String str=gson.toJson("新密码两次输入不一致！");
			out.print(str);
		}out.close();
		
	}

	private void judjeOldPwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		Gson gson=new Gson();
		PrintWriter out=response.getWriter();
		//获取客户端传来的用户ID和密码
		String userId=request.getParameter("userId");
		String oldPwd=request.getParameter("oldPwd");
		System.out.println("userID:"+userId);
		System.out.println("oldPwd:"+oldPwd);
		//根据用户ID查询密码
		IUserDao uDao=new UserDaoImpl();
		List<User> ulist = uDao.queryUserById(userId);
		for(User user:ulist){
			if(!oldPwd.isEmpty()){
			//将真实密码与输入密码做对比
			System.out.println(user.getUpassword());
			if(user.getUpassword().equals(oldPwd)){//相同继续
				String str=gson.toJson("输入密码正确可以修改！");
				out.print(str);
			}else{//不同 提示用户旧密码输入错误，无法修改
				String str=gson.toJson("输入密码错误请重新输入！");
				out.print(str);
			}out.close();
		}
		}
		
	}

	private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		//设置json编码
		response.setContentType("application/json;charset=utf-8");
		Gson gson=new Gson();
		PrintWriter out=response.getWriter();
		//获取用户信息
		String userid=request.getParameter("userId");
		String newPwd=request.getParameter("newPwd");
		//根据userid修改用户，将新密码传给用户
		int exUpdate = DBTools.exUpdate("update user set upassword=? where userid=?",newPwd, userid);
		if(exUpdate>0){
			String str="修改成功";
			request.setAttribute("uPResult",str);
			request.getRequestDispatcher("personal/crowdfunding.center/make_password.jsp").forward(request, response);
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
